# ---------------------------------------------------------------------------
# wxLua CMake bindings build file
# ---------------------------------------------------------------------------

# ---------------------------------------------------------------------------
# Project name, sets ${wxLua_SOURCE_DIR} and ${wxLua_BINARY_DIR}

project( wxLua_bindings )

# ===========================================================================
# General settings for CMake
# ===========================================================================

# ---------------------------------------------------------------------------
# Use a minimum version of CMake of 2.8, >= 2.8.3 is prefered

cmake_minimum_required( VERSION 2.8 )

# ---------------------------------------------------------------------------
# Setup the CMake environment

include( ../build/CMakewxAppLib.cmake )
include( ../build/CMakeFunctions.cmake )

# ----------------------------------------------------------------------------
# FUNCTION_ARG_PARSER( 
#       variable_name_prefix   # name to prefix variable names with
#       "FILES;DIR;PROPERTIES" # keywords to search for
#       ${ARGN} )              # separated input args 
# Usage : 
#   FUNCTION_ARG_PARSER( ABC "FILES;DIR;PROPS" FILES "a.txt" "b.txt" PROPS BBB blah )
#   Creates these local variables :
#       ABC_FILES_SET = TRUE; ABC_FILES_VALUE = "a.txt;b.txt"
#       ABC_DIR_SET   =     ; ABC_DIR_VALUE   = 
#       ABC_PROPS_SET = TRUE; ABC_PROPS_VALUE = "BBB;blah"
# ----------------------------------------------------------------------------

macro(FUNCTION_ARG_PARSER function_name arg_names )

    SET( ${function_name}_${arg}_SET )
    SET( ${function_name}_${arg}_VALUE )
    SET( arg_name_ "" )

    FOREACH(arg_ ${ARGN})
        set(a_ "")
        string(FIND "${arg_names}" "${arg_}" a_) # simple find, no regexp

        if ( ${a_} GREATER -1 )
            SET( arg_name_ "${arg_}" ) # now handling this arg_name
            SET( ${function_name}_${arg_name_}_SET TRUE )
        else()
            SET( ${function_name}_${arg_name_}_VALUE "${${function_name}_${arg_name_}_VALUE}" "${arg_}" )
        endif()        
    ENDFOREACH()

    #MESSAGE("FUNCTION_ARG_PARSER ${function_name} ${arg_names} ")
    FOREACH(arg_name_ ${arg_names})
        STRING( REGEX REPLACE "^;" "" v_ "${${function_name}_${arg_name_}_VALUE}")
        SET( ${function_name}_${arg_name_}_VALUE "${v_}" )
        #MESSAGE("    ${function_name}_${arg_name_}_VALUE= ${${function_name}_${arg_name_}_VALUE}")
    ENDFOREACH()
endmacro() 


# ===========================================================================
# Regenerate the bindings
# ===========================================================================

# ---------------------------------------------------------------------------
# This CMake script can be INCLUDE()ed to add a function ADD_WXLUABIND_GENERATION
# to (re)generate the bindings files as a make target or in Visual Studio.
#
# Author : John Labenski
#
# Usage: 
#  ADD_WXLUABIND_GENERATION(target_name
#    WXLUABIND_FILES      "*_rules.lua"    # input Lua files, either "*.lua" or an explicit list "file1.lua" "file2.lua"
#    WXLUAINTERFACE_FILES "*.i"            # input interface files, either "*.i" or an explicit list "file1.i" "file2.i"
#    WORKING_DIRECTORY    "../binding_dir" # dir to run lua genwxbind.lua in.
#    PROPERTIES FOLDER    "wxLuaBindings") # optional SET_TARGET_PROPERTIES(target_name PROPERTIES ...)
#
# Note that we put dependencies on autogenerated wxLuaBind_<foo>.dummy
# files so that this target is not run for the make all target.
# You may not be able to "build" them since the dependencies exist so
# you must either "rebuild" or run "clean" first. This is by design!
#
# ---------------------------------------------------------------------------

function(ADD_WXLUABIND_GENERATION name)

    # Variables named : ${ARG_${WXLUABIND_FILES...}_VALUE} and ${..._SET}
    FUNCTION_ARG_PARSER(ARG
                        "WXLUABIND_FILES;WXLUAINTERFACE_FILES;WORKING_DIRECTORY;PROPERTIES"
                        ${ARGN})

    set(WXLUABIND_TARGET_DEPS)  # list of dependencies for the set of files

    if ("${ARG_WORKING_DIRECTORY_VALUE}" STREQUAL "")
        set(ARG_WORKING_DIRECTORY_VALUE "${CMAKE_CURRENT_SOURCE_DIR}")
    endif()

    get_filename_component(ABS_WORKING_DIRECTORY ${ARG_WORKING_DIRECTORY_VALUE} ABSOLUTE)

    file( GLOB WXLUAINTERFACE_FILES ${ARG_WXLUAINTERFACE_FILES_VALUE} ) # get the real list of files for *.lua
    set(ARG_WXLUAINTERFACE_FILES_VALUE_ABS )
    foreach(WXLUAINTERFACE_FILE ${WXLUAINTERFACE_FILES})
        if (NOT EXISTS ${WXLUAINTERFACE_FILE})
            # Try to see if it's already absolute
            get_filename_component(ABS_WXLUAINTERFACE_FILE "${ABS_WORKING_DIRECTORY}/${WXLUAINTERFACE_FILE}" ABSOLUTE)
        else()
            set(ABS_WXLUAINTERFACE_FILE "${WXLUAINTERFACE_FILE}")
        endif()
        
        set( ARG_WXLUAINTERFACE_FILES_VALUE_ABS 
             ${ARG_WXLUAINTERFACE_FILES_VALUE_ABS} 
            "${ABS_WXLUAINTERFACE_FILE}")
    endforeach()

    file( GLOB WXLUABIND_FILES ${ARG_WXLUABIND_FILES_VALUE} ) # get the real list of files for *.lua
    set( ARG_WXLUABIND_FILES_VALUE_ABS )
    
    # sort in reverse order so that they are run in the specified order
    foreach(WXLUABIND_FILE ${WXLUABIND_FILES})
        set(WXLUABIND_FILES2 ${WXLUABIND_FILE} ${WXLUABIND_FILES2})
    endforeach()
    
    foreach(WXLUABIND_FILE ${WXLUABIND_FILES2})
        if (NOT EXISTS ${WXLUABIND_FILE})
            # Try to see if it's already absolute
            get_filename_component(ABS_WXLUABIND_FILE "${ABS_WORKING_DIRECTORY}/${WXLUABIND_FILE}" ABSOLUTE)
        else()
            set(ABS_WXLUABIND_FILE "${WXLUABIND_FILE}")
        endif()

        set( ARG_WXLUABIND_FILES_VALUE_ABS
             ${ARG_WXLUABIND_FILES_VALUE_ABS} 
            "${ABS_WXLUABIND_FILE}")

        get_filename_component(WXLUABIND_FILE_WE ${WXLUABIND_FILE} NAME_WE)

        # Use deps on autogenerated dummy files for rebuilding
        set(WXLUABIND_TARGET_DUMMY "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/wxLuaBind_${WXLUABIND_FILE_WE}.dummy")
        set(WXLUABIND_TARGET_DEPS ${WXLUABIND_TARGET_DEPS} "${WXLUABIND_TARGET_DUMMY}")

        # Initially write the dummy file so we don't try to regenerate until
        # they actually run clean first.
        file(WRITE "${WXLUABIND_TARGET_DUMMY}" 
             "Dummy build dependency file for ${ABS_WXLUABIND_FILE}")

        #@($(LUA) -e"rulesFilename=\"wxwidgets/wxadv_rules.lua\"" genwxbind.lua

        get_target_property(WXLUABIND_LUA_EXECUTABLE lua LOCATION)

        # This is the command to call protoc on each .proto file
        add_custom_command(
            OUTPUT "${WXLUABIND_TARGET_DUMMY}"
            # COMMAND ${WXLUABIND_LUA_EXECUTABLE} "-e\"rulesFileName=\\\"${ABS_WXLUABIND_FILE}\\\"\"" genwxbind.lua
            COMMAND ${WXLUABIND_LUA_EXECUTABLE} -e "rulesFilename=\"${ABS_WXLUABIND_FILE}\""  genwxbind.lua
            COMMAND echo "Dummy build dependency file for ${ABS_WXLUABIND_FILE}" > "${WXLUABIND_TARGET_DUMMY}"
            WORKING_DIRECTORY ${ARG_WORKING_DIRECTORY_VALUE}
            DEPENDS ${ABS_WXLUABIND_FILE}
            COMMENT "Running wxLua binding generator on ${WXLUABIND_FILE}"
            VERBATIM )

    endforeach()

    add_custom_target(${name} 
                      DEPENDS ${WXLUABIND_TARGET_DEPS}
                      SOURCES ${ARG_WXLUABIND_FILES_VALUE_ABS}
                              ${ARG_WXLUAINTERFACE_FILES_VALUE_ABS})
    set_target_properties(${name} PROPERTIES EXCLUDE_FROM_ALL TRUE)

    if (NOT "${ARG_PROPERTIES_VALUE}" STREQUAL "")
        SET_TARGET_PROPERTIES(${name} PROPERTIES ${ARG_PROPERTIES_VALUE} )
    endif()
endfunction()


# ---------------------------------------------------------------------------
# Create bindings for everything
# ---------------------------------------------------------------------------

ADD_WXLUABIND_GENERATION( wxluabindings 
    WXLUABIND_FILES 
        wxwidgets/wxbase_rules.lua
        wxwidgets/wxnet_rules.lua
        wxwidgets/wxcore_rules.lua
        wxwidgets/wxadv_rules.lua
        wxwidgets/wxaui_rules.lua
        wxwidgets/wxgl_rules.lua
        wxwidgets/wxhtml_rules.lua
        wxwidgets/wxmedia_rules.lua
        wxwidgets/wxpropgrid_rules.lua
        wxwidgets/wxrichtext_rules.lua
        wxwidgets/wxstc_rules.lua
        wxwidgets/wxxml_rules.lua
        wxwidgets/wxxrc_rules.lua
        wxlua/wxlua_rules.lua
        wxlua_debugger/wxluadebugger_rules.lua
        wxwidgets/wxdatatypes_rules.lua
    WXLUAINTERFACE_FILES 
        wxlua/wxlua.i
        wxlua_debugger/wxluadebugger.i
        wxwidgets/wxadv_adv.i
        wxwidgets/wxadv_grid.i
        wxwidgets/wxaui_aui.i
        wxwidgets/wxbase_base.i
        wxwidgets/wxbase_config.i
        wxwidgets/wxbase_data.i
        wxwidgets/wxbase_datetime.i
        wxwidgets/wxbase_file.i
        wxwidgets/wxcore_appframe.i
        wxwidgets/wxcore_clipdrag.i
        wxwidgets/wxcore_controls.i
        wxwidgets/wxcore_core.i
        wxwidgets/wxcore_defsutils.i
        wxwidgets/wxcore_dialogs.i
        wxwidgets/wxcore_event.i
        wxwidgets/wxcore_gdi.i
        wxwidgets/wxcore_geometry.i
        wxwidgets/wxcore_help.i
        wxwidgets/wxcore_image.i
        wxwidgets/wxcore_mdi.i
        wxwidgets/wxcore_menutool.i
        wxwidgets/wxcore_picker.i
        wxwidgets/wxcore_print.i
        wxwidgets/wxcore_sizer.i
        wxwidgets/wxcore_windows.i
        wxwidgets/wxgl_gl.i
        wxwidgets/wxhtml_html.i
        wxwidgets/wxmedia_media.i
        wxwidgets/wxnet_net.i
        wxwidgets/wxpropgrid_propgrid.i
        wxwidgets/wxrichtext_richtext.i
        wxwidgets/wxstc_stc.i
        wxwidgets/wxxml_xml.i
        wxwidgets/wxxrc_xrc.i
    WORKING_DIRECTORY "${wxLua_ROOT_DIR}/bindings" 
    PROPERTIES FOLDER "wxLua/bindings"
    )
